<template>
	<view>
		<!-- 使用自定义的组件search -->
		<search :bgcolor="'#c3c5c1'" :radius="18" @myclick="gotoSearch"></search>
		<view class="scroll-view-container">
			<!-- 左侧区域 -->
			<scroll-view scroll-y="true" :style="{height:windowH + 'px'}" class="left-scroll-view">

				<!-- block标签就是站位但不渲染 -->
				<block v-for="(item,i) in catelist" :key="i">
					<view :class="['left-scroll-view-item', i === active ? 'active' : '']" @click="activeChange(i)">
						{{item.cat_name}}</view>
				</block>
			</scroll-view>
			<!-- 右侧区域 -->
			<scroll-view scroll-y="true" :style="{height:windowH + 'px'}" :scroll-top="scrolltop">
				<view class="cate-lv2" v-for="(item2,i2) in catelist2" :key="i2">
					<view class="cate-lv2-title">/ {{item2.cat_name}} /</view>
					<!-- 三级分类的列表循环结构 -->
					<view class="cate-lv3-box" >
						<view class="cate-lv3-item" v-for="(item3,i3) in item2.children" :key="i3" @click="gotoGoodsList(item3)">
							<image :src="item3.cat_icon" ></image>
							<text>{{item3.cat_name}}</text>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	//使TabBar页面的购物车也显示商品的数量 导入mixins的TabBar-badge文件
	import badgeMix from '@/mixins/tabbar-badge.js'
	export default {
		mixins:[badgeMix],
		data() {
			return {
				//可使用的高度
				windowH: 0,
				//一级列表数据
				catelist: [],
				active: 0,
				//二级分类列表
				catelist2: [],
				scrolltop:0
			};
		},
		onLoad() {
			//这个方法可以获取手机的信息
			const sysInfo = uni.getSystemInfoSync()
			console.log(sysInfo)
			this.windowH = sysInfo.windowHeight - 60

			//调用获取数据的方法
			this.getCateList()
		},
		methods: {
			//获取类表数据
			async getCateList() {
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/categories')
				console.log(res)
				if (res.meta.status !== 200) return uni.$showMsg()
				this.catelist = res.message
				//获取二级分类
				this.catelist2 = res.message[this.active].children
			},
			//点击改变active 一级分类的值
			activeChange(i) {
				this.active = i

				//每一次切换都为二级分类赋值
				this.catelist2 = this.catelist[i].children
				
				//每次改变一级分类就将scrolltop变为0,注意，两次赋值相同时，不会有改变
				this.scrolltop = this.scrolltop === 0 ? 0.1 : 0
			},
			//跳转到商品列表页面
			gotoGoodsList(item){
				uni.navigateTo({
					url:'/subpkg/goods_list/goods_list?cid=' + item.cat_id
				})
			},
			//跳转到搜索页面
			gotoSearch(){
				console.log('组件调用页面给的方法')
				uni.navigateTo({
					url:'/subpkg/search/search'
				})
			}
		}
	}
</script>

<style lang="scss">
	.scroll-view-container {
		display: flex;
	}

	.left-scroll-view {
		width: 240rpx;
	}

	.left-scroll-view-item {
		background-color: #f7f7f7;
		line-height: 60px;
		text-align: center;
		font-size: 16px;

		&.active {
			position: relative;
			background-color: #fff;

			&::before {
				position: absolute;
				top: 50%;
				transform: translateY(-50%);
				content: '';
				display: block;
				width: 3px;
				height: 30px;
				background-color: #c00000;
			}
		}

	}

	.cate-lv2-title {
		font-size: 14px;
		font-weight: bold;
		text-align: center;
		padding: 15px 0;
	}
	.cate-lv3-box{
		display: flex;
		flex-wrap: wrap;
		
		.cate-lv3-item{
			width: 33.3%;
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			margin-bottom: 10px;
			image{
				width: 60px;
				height: 60px;
			}
			text{
				font-size: 12px;
			}
		}
	}
	
</style>
